package org.openjava.jsonbuilder;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class PojoNodeConfig {
	private String quote = "\"";
	private String configStr[];
	private String config;
	//private String[] metaChar = {"!"};
	private boolean mode = INCLUDE_MODE;
	private static final boolean INCLUDE_MODE = true;
	private static final boolean EXCLUDE_MODE = false;
	
	public PojoNodeConfig(String quote,String config) {
		this.quote = quote;
		// TODO Auto-generated constructor stub
		config(config);
	}
	
	public PojoNodeConfig() {
		// TODO Auto-generated constructor stub
	} 

	public PojoNodeConfig(String config) {
		this.quote = "\"";
		// TODO Auto-generated constructor stub
		config(config);
	}
	
	private void  config(String config){
		if(config.indexOf("!") != -1){
			config = config.substring(1);
			mode = EXCLUDE_MODE;
		}
		List<String> list = Arrays.asList(config.split(";"));
		Iterator<String> iter = list.iterator();
		while(iter.hasNext()){
			if(iter.next().trim().length() == 0){
				iter.remove();
			}
		}
		this.configStr =  (String[]) list.toArray();
	}
	public boolean isIncluded(String quatifiedName){
		boolean result = false;
		result = fitDegree(quatifiedName) != NO_FIT;
		return result;
	}
	
	
	public static final int PERFECT_FIT = 0;
	public static final int PART_FIT = 1;
	public static final int NO_FIT = 2;
	
	public int fitDegree(String quatifiedName){
		int fitDegree;
		String[] nameFlags = quatifiedName.split("\\.");
		if(mode){
			fitDegree= NO_FIT;
			for(int i = 0,j = configStr.length;i < j;i ++){
				String meta = configStr[i];
				String[] metaFlags = meta.split("\\.");
				int k = 0;
				for(int h = nameFlags.length;k < h;k ++){
					if(k >= metaFlags.length){
						break;
					}
					if(nameFlags[k].equals(metaFlags[k])|| metaFlags[k].equals("*")){
						continue;
					}
					break;
				}
				if(metaFlags.length == nameFlags.length  ){
					if(k == nameFlags.length ){
						fitDegree = PERFECT_FIT;
					}
				} else if(metaFlags.length < nameFlags.length){
					if(k == metaFlags.length){
						fitDegree = PART_FIT;
					}
				}
				else if(metaFlags.length > nameFlags.length ){
					if(k == nameFlags.length){
						fitDegree = PART_FIT;
					} 
				}
				
				if(fitDegree != NO_FIT){
					break;
				}
			}
		}else{
			fitDegree = PERFECT_FIT;
			for(int i = 0,j = configStr.length;i < j;i ++){
				String meta = configStr[i];
				String[] metaFlags = meta.split("\\.");
				int k = 0;
				for(int h = nameFlags.length;k < h;k ++){
					if(k >= metaFlags.length)
						break;
					if(nameFlags[k].equals(metaFlags[k])|| metaFlags[k].equals("*") ){
						continue;
					}
					break;
				}
				if(metaFlags.length == nameFlags.length  ){
					if(k == nameFlags.length){
						fitDegree = NO_FIT;
					}
				}else if(metaFlags.length < nameFlags.length){
					if(k == metaFlags.length){
						fitDegree = PART_FIT;
					}
				}
				else if(metaFlags.length > nameFlags.length ){
					if(k == nameFlags.length){
						fitDegree = PART_FIT;
					} 
				}
				if(k != PERFECT_FIT){
					break;
				}
				
			}
		}
		return fitDegree;
	}
	/*
	public boolean isQuatifiedContains(String quatifiedName){
		boolean result = false;
		for(int i = 0,j = configStr.length;i < j;i ++){
			if(isPerfectContains(configStr[i], quatifiedName)){
				result = true;
				break;
			}
		}
		return result;
	}
	public boolean isPerfectContains (String meta,String quatifiedName){
		boolean result ;
		String[] metaflags  = meta.split("\\.") ;
		String[] nodeNameFlags = quatifiedName.split("\\.");
		if(mode){
			result= true;
		}else{
			result = false;
		}
		int i = 0,j = nodeNameFlags.length;
		for(;i < j;i ++){
			if(metaflags[i].equals(nodeNameFlags[i]) || (metaflags[i].equals("*"))){
				continue;
			}
			result = !result;
			break;
		}
		if(i == j){
			if(!mode){
				result = false;
			}
		}
		return result;
	}*/
	/*private boolean isContains(String meta,String quatifiedName){
		boolean result ;
		String[] metaflags  = meta.split("\\.") ;
		String[] nodeNameFlags = quatifiedName.split("\\.");
		if(mode){
			result= true;
		}else{
			result = false;
		}
		int i = 0,j = nodeNameFlags.length;
		for(;i < j;i ++){
			if(metaflags[i].equals(nodeNameFlags[i]) || (metaflags[i].equals("*"))){
				continue;
			}
			result = !result;
			break;
		}
			
		return result;
		
	}*/
	public String getQuote() {
		return quote;
	}

	public void setQuote(String quote) {
		this.quote = quote;
	}
	
	
	public String getConfig() {
		return config;
	}

	public void setConfig(String config) {
		this.config = config;
		config(config);
	}

	public static void main(String[] args) {
		PojoNodeConfig config = new PojoNodeConfig("1.2.3.*;1.2.3;");
		System.out.println(config.fitDegree("1.2.3"));
	}
}
